import 'package:ls_work/mvp/base_page_presenter.dart';
import 'package:ls_work/net/net.dart';
import 'package:ls_work/a_yyt/page/care_search_page.dart';
import 'package:ls_work/net/post_request.dart';
import 'package:ls_work/widgets/state_layout.dart';
import 'dart:convert';
import 'package:ls_work/net/HttpUtil.dart';
import 'package:ls_work/a_yyt/models/care_report_entity.dart';
import 'package:dio/dio.dart';

class CareSearchPresenter extends BasePagePresenter<CareSearchPageState> {
  Future fectData(
    bool isShowDialog, {
    String? elderNameLike,
    String? createDateStart,
    String? createDateEnd,
    String? serviceAreaIds,
    int page = 1,
  }) async {
    Map<String, dynamic> map = {
      'page': page,
      'rows': 20,
      'elderNameLike': elderNameLike,
      'createDateStart': createDateStart,
      'createDateEnd': createDateEnd,
      'serviceAreaIds': serviceAreaIds,
    };
    view.showProgress();
    HttpUtil.instance
        .getDio()
        .post('${HttpApi.nurseBeanList}', data: map,options: PostRequest.urlencodedHeader)
        .then((r) {
      ///
      view.closeProgress();
      if (r.statusCode == 200) {
        Map<String, dynamic> map = json.decode(r.toString());
        if (map["code"].toString() == "0") {
          if (map['data'] != null) {
            CareReportEntity model = CareReportEntity.fromJson(map['data']);
            if ((model.rows??[]).length > 0) {
              view.provider.setHasMore((model.rows??[]).length == 20);
              if (page == 1) {
                /// 刷新
                view.provider.list.clear();
                view.provider.addAll(model.rows??[]);
              } else {
                view.provider.addAll(model.rows??[]);
              }
            } else {
              if (page == 1) {
                /// 刷新
                view.provider.list.clear();
              }
              view.provider.setStateType(StateType.empty);
            }
          } else {
            view.showToast(map["msg"]);
          }
        }
      } else {
        view.showToast(r.statusCode.toString());
        view.provider.setHasMore(false);
        view.provider.setStateType(StateType.empty);
      }
    }).catchError((e) {
      view.closeProgress();
      print(e.toString());
      view.provider.setHasMore(false);
      view.provider.setStateType(StateType.empty);
      view.showToast(getErrorMessage((e as DioError).type));
    });
  }
}
